package com.xxx;

import com.xxx.bean.ConsumerComponent;
import com.xxx.config.ConsumerConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;
import java.util.concurrent.*;


public class ConcurrentConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        ThreadPoolExecutor threadPoolExecutor = createThreadPoolExecutor();
        System.out.println("=========Input something on console for starting invoke");

        System.in.read();
        while (true) {


            Thread.sleep(8);

            threadPoolExecutor.submit(() -> {
                try {
                    //调用testingOne
                    service.testingOne();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            threadPoolExecutor.submit(() -> {
                try {
                    service.testingTwo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            threadPoolExecutor.submit(() -> {
                try {
                    service.testingThree();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        }
    }


    public static ThreadPoolExecutor createThreadPoolExecutor() {
        int corePoolSize = 50;
        int maximumPoolSize = 50;
        long keepAliveTime = 60;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);

        return new ThreadPoolExecutor
                (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, (r, executor)
                        -> System.out.println("======Too many requests do abort"));
    }


}
